package org.mch.func.db.pdmfile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.mch.comm.lang.MDate;
import org.mch.comm.utils.MTools;
import org.mch.comm.velocity.MVelocityEngine;
import org.mch.comm.velocity.VelBeanData;
import org.mch.comm.velocity.VelFieldData;

public class PDMTools {
	private static MTools tool = new MTools();

	public static List<PDMTableObject> getPdmTableObject(File file) throws Exception {

		if (file == null) {
			return null;
		}
		PDMTableObjectTool bpcvo = new PDMTableObjectTool(file);

		return bpcvo.buildVOList();
	}

	public static StringBuffer getHibernateVO(PDMTableObject pdmto, String fieldtype) {
		if (pdmto == null) {
			return new StringBuffer("");
		}
		StringBuffer sb = new StringBuffer("");

		StringBuffer gs = new StringBuffer("");

		PDMTableColumn[] cols = pdmto.getCols();
		String tablecode = pdmto.getTableCode();
		tablecode = tool.firstLetterUpperCase(tablecode) + "VO";

		sb.append("\n");
		sb.append("import javax.persistence.Column;\n");
		sb.append("import javax.persistence.Id;\n\n");
		sb.append("import javax.persistence.Entity;\n");
		sb.append("import javax.persistence.Table;\n\n");
		sb.append("import jll.base.entity.vo.BaseVO;\n\n");
		sb.append("/**\n");
		sb.append(" * 描述：；<BR>\n");
		sb.append(" * \n");
		sb.append(" * 类名：" + tablecode + ".java<BR>\n");
		sb.append(" * \n");
		sb.append(" * @author MC；Date：" + new MDate(new Date()) + "<BR>\n");
		sb.append(" * \n");
		sb.append(" *         说明：此数据对象类为ToolsResultSet工具自动生成；<BR>\n");
		sb.append(" * <BR>\n");
		sb.append(" */\n");
		sb.append("@Entity\n");
		sb.append("@Table(name = \"");
		sb.append(pdmto.getTableCode());
		sb.append("\")\n");
		sb.append("public class ");
		sb.append(tablecode);
		sb.append(" extends BaseVO {\n\n");
		sb.append("\t/**\n");
		sb.append("\t * \n");
		sb.append("\t */\n");
		sb.append("\tprivate static final long serialVersionUID = 1L;\n\n");

		int k = 0;
		for (int count = cols.length; k < count; k++) {
			PDMTableColumn col = cols[k];
			sb.append("\t/**\n");
			sb.append("\t * " + col.getName() + "；\n");
			sb.append("\t */\n");
			if (col.isPkFlag()) {
				sb.append("\t@Id\n");

				gs.append("\tpublic String getPk() {\n");
				gs.append("\t\treturn this." + col.getCode() + ";\n");
				gs.append("\t}\n\n");

				gs.append("\tpublic void setPk(String pk) {\n");
				gs.append("\t\tthis." + col.getCode() + " = pk;\n");
				gs.append("\t}\n\n");
			} else {
				sb.append("\t@Column\n");
			}
			sb.append("\t" + fieldtype);
			sb.append(" ");
			sb.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			sb.append(" ");
			sb.append(col.getCode());
			sb.append(";\n\n");

			gs.append("\tpublic ");
			gs.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			gs.append(" get");
			gs.append(tool.firstLetterUpperCase(col.getCode()) + "() {\n");
			gs.append("\t\treturn this." + col.getCode() + ";\n");
			gs.append("\t}\n\n");
			gs.append("\tpublic void set");
			gs.append(tool.firstLetterUpperCase(col.getCode()) + "(");
			gs.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			gs.append(" " + col.getCode() + ") {\n");
			gs.append("\t\tthis." + col.getCode() + " = " + col.getCode() + ";\n");
			gs.append("\t}\n\n");
		}

		sb.append(gs);

		sb.append("\tpublic String getTableType() {\n");
		sb.append("\t\treturn null;\n");
		sb.append("\t}\n\n");

		sb.append("\tpublic String getTableName() {\n");
		sb.append("\t\treturn \"" + pdmto.getTableCode() + "\";\n");
		sb.append("\t}\n\n");

		sb.append("}\n");

		return sb;
	}

	public static String buildJavaBean(PDMTableObject pdmto, String fieldtype, String packageName, String authorName,
			boolean isnewts) throws Exception {
		VelBeanData beanData = new VelBeanData();
		beanData.setPackageName(packageName == null ? "" : "package " + packageName);
		beanData.setAuthorName(authorName == null ? "" : authorName);

		PDMTableColumn[] cols = pdmto.getCols();
		String tablecode = pdmto.getTableCode();
		tablecode = tool.firstLetterUpperCase(tablecode) + "VO";

		beanData.setClassName(tablecode);

		beanData.setCreateDate(new MDate(new Date()).toString());

		beanData.setVersionUID(tool.getSerialVersionUID() + "L");

		List<VelFieldData> fieldList = new ArrayList<>();

		for (int k = 0, count = cols.length; k < count; k++) {
			PDMTableColumn col = cols[k];

			if (!isnewts && "ts".equals(col.getCode())) {
				continue;
			}

			VelFieldData fieldData = new VelFieldData();
			fieldData.setFieldDesc(col.getName());
			fieldData.setFieldModifier(fieldtype);
			fieldData.setFieldType(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			fieldData.setFieldName(col.getCode());

			fieldData.setMethodGetName("get" + tool.firstLetterUpperCase(col.getCode()));
			fieldData.setMethodSetName("set" + tool.firstLetterUpperCase(col.getCode()));

			fieldList.add(fieldData);
		}

		beanData.setFieldList(fieldList);

		return new MVelocityEngine().getTemplateStrEO("resource/vmtemplate/JavaBean.vm", beanData);
	}

	public static String buildEntityEO(PDMTableObject pdmto, String fieldtype, String packageName, String authorName,
			boolean isnewts) throws Exception {
		VelBeanData beanData = new VelBeanData();
		beanData.setPackageName(StringUtils.isEmpty(packageName) ? "" : "package " + packageName + ";");
		beanData.setAuthorName(authorName == null ? "" : authorName);

		PDMTableColumn[] cols = pdmto.getCols();
		String tablecode = pdmto.getTableCode();
		beanData.setTableName(tablecode);
		beanData.setPKFieldName(pdmto.getPkField() == null ? "null" : pdmto.getPkField());
		tablecode = tool.firstLetterUpperCase(tablecode) + "VO";

		beanData.setClassName(tablecode);

		beanData.setCreateDate(new MDate(new Date()).toString());

		beanData.setVersionUID(tool.getSerialVersionUID() + "L");

		List<VelFieldData> fieldList = new ArrayList<>();

		for (int k = 0, count = cols.length; k < count; k++) {
			PDMTableColumn col = cols[k];

			if (!isnewts && "ts".equals(col.getCode())) {
				continue;
			}

			VelFieldData fieldData = new VelFieldData();
			fieldData.setFieldDesc(col.getName());
			fieldData.setFieldModifier(fieldtype);
			fieldData.setFieldType(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			fieldData.setFieldName(col.getCode());

			fieldData.setMethodGetName("get" + tool.firstLetterUpperCase(col.getCode()));
			fieldData.setMethodSetName("set" + tool.firstLetterUpperCase(col.getCode()));

			fieldList.add(fieldData);
		}

		beanData.setFieldList(fieldList);

		return new MVelocityEngine().getTemplateStrEO("resource/vmtemplate/MCEntityEO.vm", beanData);
	}

	public static StringBuffer getJavaBean(PDMTableObject pdmto, String fieldtype) {
		if (pdmto == null) {
			return new StringBuffer("");
		}
		StringBuffer sb = new StringBuffer("");

		StringBuffer gs = new StringBuffer("");

		PDMTableColumn[] cols = pdmto.getCols();
		String tablecode = pdmto.getTableCode();
		tablecode = tool.firstLetterUpperCase(tablecode) + "VO";

		sb.append("\n\n");
		sb.append("import java.io.Serializable;\n\n");
		sb.append("/**\n");
		sb.append(" * 描述：；<BR>\n");
		sb.append(" * \n");
		sb.append(" * 类名：" + tablecode + ".java<BR>\n");
		sb.append(" * \n");
		sb.append(" * @author MC；DateTime：" + new MDate(new Date()) + "<BR>\n");
		sb.append(" * \n");
		sb.append(" *         说明：此数据对象类为ToolsResultSet工具自动生成；<BR>\n");
		sb.append(" * <BR>\n");
		sb.append(" */\n");
		sb.append("public class " + tablecode + " implements Serializable {\n\n");
		sb.append("\t/**\n");
		sb.append("\t* \n");
		sb.append("\t*/\n");
		sb.append("\tprivate static final long serialVersionUID = 1L;\n\n");

		int k = 0;
		for (int count = cols.length; k < count; k++) {
			PDMTableColumn col = cols[k];
			sb.append("\t/**\n");
			sb.append("\t * " + col.getName() + "；\n");
			sb.append("\t */\n\t");
			sb.append(fieldtype);
			sb.append(" ");
			sb.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			sb.append(" ");
			sb.append(col.getCode());
			sb.append(";\n\n");

			gs.append("\tpublic ");
			gs.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			gs.append(" get" + tool.firstLetterUpperCase(col.getCode()) + "() {\n");
			gs.append("\t\treturn this." + col.getCode() + ";\n");
			gs.append("\t}\n\n");
			gs.append("\tpublic void set" + tool.firstLetterUpperCase(col.getCode()) + "(");
			gs.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			gs.append(" " + col.getCode() + ") {\n");
			gs.append("\t\tthis." + col.getCode() + " = " + col.getCode() + ";\n");
			gs.append("\t}\n\n");
		}

		sb.append(gs);
		sb.append("}\n\n\n");

		return sb;
	}

	public static String test() throws Exception {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("test", "中国人");

		VelocityContext context = new VelocityContext();
		context.put("vo", map);

		String vmfile = "/vm/valueobject/ttt_utf8.vm";

		String str = new MVelocityEngine("utf-8", "utf-8").getTemplateStr(vmfile, context);

		return str;
	}

	public static StringBuffer getMCHBaseEO(PDMTableObject pdmto, String fieldtype, String extendEO) {
		return getMCHBaseEO(pdmto, fieldtype, extendEO, false);
	}

	public static StringBuffer getMCHBaseEO(PDMTableObject pdmto, String fieldtype, String extendEO, boolean isnewts) {
		if (pdmto == null) {
			return new StringBuffer("");
		}
		StringBuffer sb = new StringBuffer("");

		StringBuffer gs = new StringBuffer("");

		PDMTableColumn[] cols = pdmto.getCols();
		String tablecode = pdmto.getTableCode();
		tablecode = tool.firstLetterUpperCase(tablecode) + "DO";

		sb.append("\n\n");
		sb.append("import ").append(extendEO).append(";\n\n");
		sb.append("/**\n");
		sb.append(" * 描述：");
		sb.append(pdmto.getTableName());
		sb.append("（表对象）；<BR>\n");
		sb.append(" * \n");
		sb.append(" * 类名：" + tablecode + ".java<BR>\n");
		sb.append(" * \n");
		sb.append(" * @author MC；<BR>\n");
		sb.append(" * \n");
		sb.append(" *         说明：此数据对象类为ToolsResultSet工具自动生成；<BR>\n");
		sb.append(" * <BR>\n");
		sb.append(" */\n");
		sb.append("public class ");
		sb.append(tablecode);
		sb.append(" extends ").append(extendEO.substring(extendEO.lastIndexOf(".") + 1, extendEO.length())).append(" {\n");
		sb.append("\tprivate static final long serialVersionUID = ").append(tool.getSerialVersionUID()).append("L;\n\n");

		int k = 0;
		for (int count = cols.length; k < count; k++) {
			PDMTableColumn col = cols[k];
			if (isnewts == false && col.getCode().equalsIgnoreCase("ts")) {
				continue;
			}
			sb.append("\t/**\n");
			sb.append("\t * " + col.getName() + "；\n");
			sb.append("\t */\n\t");
			sb.append(fieldtype);
			sb.append(" ");
			sb.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			sb.append(" ");
			sb.append(col.getCode());
			sb.append(";\n\n");

			gs.append("\t/**\n");
			gs.append("\t * 返回：" + col.getName() + "；\n");
			gs.append("\t */\n");
			gs.append("\tpublic ");
			gs.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			gs.append(" get" + tool.firstLetterUpperCase(col.getCode()) + "() {\n");
			gs.append("\t\treturn this." + col.getCode() + ";\n");
			gs.append("\t}\n\n");

			gs.append("\t/**\n");
			gs.append("\t * 设置：" + col.getName() + "；\n");
			gs.append("\t */\n");
			gs.append("\tpublic void set" + tool.firstLetterUpperCase(col.getCode()) + "(");
			gs.append(PdmTypeConvert.getJavaType(col.type, col.length, col.precision));
			gs.append(" " + col.getCode() + ") {\n");
			gs.append("\t\tthis." + col.getCode() + " = " + col.getCode() + ";\n");
			gs.append("\t}\n\n");
		}

		sb.append(gs);

		sb.append("\t@Override\n");
		sb.append("\tpublic String getPKFieldName() {\n");
		sb.append("\t\treturn \"" + pdmto.getPkField() + "\";\n");
		sb.append("\t}\n\n");

		sb.append("\t@Override\n");
		sb.append("\tpublic String getTableName() {\n");
		sb.append("\t\treturn \"" + pdmto.getTableCode() + "\";\n");
		sb.append("\t}\n\n");

		sb.append("\t@Override\n");
		sb.append("\tpublic String getPrimaryKey() {\n");
		sb.append("\t\treturn this." + pdmto.getPkField() + ";\n");
		sb.append("\t}\n\n");

		sb.append("\t@Override\n");
		sb.append("\tpublic void setPrimaryKey(String pk) {\n");
		sb.append("\t\tthis." + pdmto.getPkField() + "=pk;\n");
		sb.append("\t}\n\n");

		sb.append("\t@Override\n");
		sb.append("\tpublic String getParentPKFieldName() {\n");
		sb.append("\t\treturn null;\n");
		sb.append("\t}\n\n");

		sb.append("}\n\n\n");

		return sb;
	}
}